#ifndef __SNAKE__
#define __SNAKE__

#include <vector>
#include <queue>

#include "eobject.hpp"
#include "GameMap.hpp"
#include "Fruit.hpp"
#include "Tools.hpp"
#include "Constant.hpp"

struct SnakeInfo
{
    int _direction;
    POS Body[3];
};

extern std::vector<SnakeInfo> SnakeList;
class SnakeBase
{
protected:
    static int _Collision[MAP_SIZE_X][MAP_SIZE_Y];
    static int _Type[MAP_SIZE_X][MAP_SIZE_Y];
    static int _Updated;
    static int _counter;

private:
    friend class SnakeOperation;
};

class Snake : public EObject, private SnakeBase
{
public:
    Snake();
    //蛇的长度
    int length() const;

    //蛇的绘制
    void draw() final;

    //判断是否可以向某个方向前进
    bool turn_check(enum Orientation _dir);

    //改变前进方向
    void turn_to(enum Direction _dir);
    void turn_to(enum Orientation _dir);

    //获取当前行进的绝对方向
    int get_direction();

    void update() final;

    void lateupdate() final;

    //获取蛇的编号
    int getid() const;

    //己方被杀死后敌方得到的分数（我愿称其为内鬼函数）
    int getscore();

    //杀死自己
    void kill();

private:
    //蛇的前进方向
    int _direction;
    //蛇体
    std::deque<POS> Body;
    //蛇的存活状态
    bool alive;
    //蛇的前进速度（update次数/s）
    double speed;
    //是否为AI
    bool isAI;
    int score;
    int _id;
    int killedby;
    friend class SnakeOperation;
};

class SnakeOperation
{
public:
    SnakeOperation(SnakeOperation &) = delete;
    SnakeOperation &operator=(const SnakeOperation &) = delete;
    static SnakeOperation &Singleton();
    void update();
    void lateUpdate();
    shared_ptr<Snake> CreateSnake();
    void KillSnake(int __snkid);

private:
    SnakeOperation() = default;
    ~SnakeOperation() = default;
    static std::vector<std::shared_ptr<Snake>> __snakes;
    bool Snake_Comparer(const Snake &a,const Snake &b);
};

#endif
